package com.example.cet4try.activity;

import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

import com.example.cet4try.R;
import com.example.cet4try.Utils.Tools;
import com.example.cet4try.Utils.WordData;
import com.example.cet4try.myDialog.WordDetailsDialog;

import java.util.ArrayList;

public class ActivityBlanksSpelling extends BaiduTTSActivity {

    private Button[] buttons = new Button[6];
    private TextView spelling_show;
    private WordData wordData;
    private String english;
    private String[] rest = new String[]{"",""};
    private ArrayList<String> btsText = new ArrayList<String>();
    private WordDetailsDialog wordDetailsDialog;
    private boolean endflag = false,wrongFlag = false;
    private String username;
    private int selectButtonId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blanks_spelling);

        initView();

        SharedPreferences preferences = getSharedPreferences("App_Shared_Data", 0);
        username = preferences.getString("username","");

        Button btn_rs = findViewById(R.id.renshi);
        btn_rs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Tools.AddUserData(username,wordData.getMid(),"already");
                reStart();
            }
        });
        Button langdu = findViewById(R.id.langdux);
        langdu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                speak();
            }
        });

        int id = Tools.MyRandom(1,4493);
        wordData = Tools.souID(id);

        english = wordData.getEnglish();
        TextView chinese_show = findViewById(R.id.chinese_show1);
        chinese_show.setText(wordData.getChinese().replaceAll("#","\n"));

        wordDetailsDialog = new WordDetailsDialog("拼写填空");
        wordDetailsDialog.setPlaySound(new WordDetailsDialog.PlaySound() {
            @Override
            public void speak_d() {
                speak();
            }
        });

        int keylen = 1;
        switch (english.length()){
            case 1:
            case 2: break;
            case 3:
                keylen = 2; break;
            default:
                keylen = 3;break;
        }
        int index = Tools.MyRandom(0,english.length()-keylen);
        String key = english.substring(index, index + keylen);
        btsText.add(key);
        rest[0] = english.substring(0,index);
        rest[1] = english.substring(index+keylen);

        String textShow = english.replaceFirst(key,"___");

        spelling_show.setText(textShow);

        int dui = Tools.MyRandom(0, 5);
        setButtonText(dui, key);

        for (int i=0;i<6;i++){
            if (i== dui)
                continue;
            setButtonText(i,getKey_f(keylen));
        }

        Button next = findViewById(R.id.next_pin);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reStart();
            }
        });

    }

    private void initView(){
        buttons[0] = findViewById(R.id.button1);
        buttons[1] = findViewById(R.id.button2);
        buttons[2] = findViewById(R.id.button3);
        buttons[3] = findViewById(R.id.button4);
        buttons[4] = findViewById(R.id.button5);
        buttons[5] = findViewById(R.id.button6);
        for (int i=0;i<buttons.length;i++){
            buttons[i].setOnClickListener(new button_spelling_onclick());
            buttons[i].setTag(i);
        }
        spelling_show = findViewById(R.id.spelling_show);

    }

    private void setButtonText(int i,String text){
        buttons[i].setText(text);
    }

    private String getKey_f(int len){
        if (len>1)
            len = Tools.MyRandom(1,len);
        StringBuilder temp = new StringBuilder();
        for (int i=0;i<len;i++){
            temp.append(get_a_z());
        }
        String res = temp.toString();
        if (btsText.contains(res))
            return getKey_f(len);
        else {
            btsText.add(res);
            return res;
        }
    }

    private char get_a_z(){
        int ch = Tools.MyRandom('a','z');
        return (char) ch;
    }

    private class button_spelling_onclick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            if (endflag)
                return;
            AddTimes();

            Button button = (Button)v;

            String s = button.getText().toString();

            if (selectButtonId != -1){
                buttons[selectButtonId].setTextColor(0xff333333);
                buttons[selectButtonId].setBackground(getResources().getDrawable(R.drawable.button_spelling1));
            }
            String spelling_text;
            int tag = (int) button.getTag();
            if (tag == selectButtonId){
                selectButtonId = -1;
                spelling_text = rest[0]+"___"+rest[1];
                spelling_show.setText(spelling_text);
            }
            else {
                selectButtonId = tag;
                spelling_text = rest[0]+s+rest[1];
                button.setTextColor(0xffffffff);
                button.setBackground(getResources().getDrawable(R.drawable.button_spelling2));
                spelling_show.setText(spelling_text);
                if (english.equals(spelling_text)){
                    endflag = true;
                    if (!wrongFlag)
                        AddRight();
                    Drawable yesicon = getResources().getDrawable(R.drawable.yes);
                    yesicon.setBounds(0,0,90,90);
                    spelling_show.setCompoundDrawables(null, null, yesicon, null);
                    wordDetailsDialog.setData(wordData);
                    wordDetailsDialog.show(getSupportFragmentManager(),"WordDetailsDialog");
                }
                else{
                    wrongFlag = true;
                    String t = "<font color='#D0021B'>" + spelling_text + "</font>";
                    spelling_show.setText(Html.fromHtml(t));
                    Animation shake = AnimationUtils.loadAnimation(ActivityBlanksSpelling.this,R.anim.shake2);
                    spelling_show.startAnimation(shake);
                    Tools.AddUserData(username,wordData.getMid(),"wrong");
                }

            }



        }
    }

    private void speak() {
        super.speak(wordData.getEnglish());
    }



}
